#Extract date variable from SPSS file and later merge it to .dta - set
#install required packages if you do not already have them

list.of.packages <- c("haven", "foreign", "readstata13")
new.packages <- list.of.packages[!(list.of.packages %in% installed.packages()[,"Package"])]
if(length(new.packages)) install.packages(new.packages)


#load libraries
library(haven)
library(foreign)
library(readstata13)

#INSERT YOUR PATH HERE 
setwd(" ")
##################################################################################################
for (i in 93:121){
  #wave 114 is missing
  if (i==114) {
    next
  }
  
  w <- as.character(i)
  
  data <- read_sav(paste0("VOX/vox_spss/vox",w,".sav"))
  
 
  intdate <- data$DATE
  #some waves stored interview number differently
  if (i==99 | i==100){
    nr <- data$nummer
  } else if (i==116){
    nr <- data$NR
  }
  else {
  nr <- data$nr
  }
  
  voxnr <- i
  
  dataframe <- data.frame(intdate, nr, voxnr)
  
  save.dta13(dataframe, paste0("VOX/vox_dta/workingfiles/intdate_vox",w,".dta"), convert.dates=T) 
}



